package cloud.maque.module.system.service.impl;

import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.common.core.utils.TokenUserUtil;
import cloud.maque.module.system.entity.SysRole;
import cloud.maque.module.system.mapper.RoleMapper;
import cloud.maque.module.system.service.RoleService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

/**
 * 角色的相实数据库实现
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, SysRole> implements RoleService {


    @Autowired
    TokenUserUtil userUtil;

    @Override
    public List<SysRole> GetList(SysRole data) {
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        if (!StrUtil.isEmptyIfStr(data.getRoleName())) {
            query.lambda().like(SysRole::getRoleName, data.getRoleName());
        }

        if (!StrUtil.isEmptyIfStr(data.getRoleKey())) {
            query.lambda().like(SysRole::getRoleKey, data.getRoleKey());
        }

        query.lambda().orderByAsc(SysRole::getRoleSort);
        return this.list(query);
    }

    @Override
    public SysRole SaveRole(SysRole data) {
        Assert.hasText(data.getRoleName(), "角色名称不能为空");
        Assert.hasText(data.getRoleKey(), "角色编码不能为空");


        if (StrUtil.isEmptyIfStr(data.getRoleId())) {
            data.setCreateBy(userUtil.GetUserName());
            data.setCreateTime(new Date());
            data.setUpdateBy(data.getCreateBy());
            data.setUpdateTime(new Date());
        } else {
            SysRole oldPost = this.baseMapper.selectById(data.getRoleId());
            if (oldPost != null) {
                data.setCreateTime(oldPost.getCreateTime());
                data.setCreateBy(oldPost.getCreateBy());
            }
            data.setUpdateBy(userUtil.GetUserName());
            data.setUpdateTime(new Date());

        }

        if (!StrUtil.isEmpty(data.getRoleKey())) {
            SysRole oldPost = GetByKey(data.getRoleKey());
            if (oldPost != null && !oldPost.getRoleId().equals(data.getRoleId())) {
                throw new BusinessException("角色编码已存在");
            }
        }

        this.saveOrUpdate(data);


        return data;
    }


    @Override
    public Boolean DeleteRole(String roleId) {
        return this.removeById(roleId);
    }

    @Override
    public SysRole EditStatus(SysRole data) {
        Assert.hasText(data.getRoleId(), "ID不能为空");

        SysRole post = this.getById(data.getRoleId());
        if (post == null) {
            throw new BusinessException("角色不存在");
        }
        if ("1".equals(post.getStatus())) {
            post.setStatus("0");
        } else {
            post.setStatus("1");
        }
        UpdateWrapper<SysRole> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysRole::getRoleId, post.getRoleId());
        updateWrapper.lambda().set(SysRole::getStatus, post.getStatus());
        updateWrapper.lambda().set(SysRole::getUpdateBy, userUtil.GetUserName());
        updateWrapper.lambda().set(SysRole::getUpdateTime, new Date());
        this.update(updateWrapper);
        return post;
    }

    @Override
    public SysRole GetByKey(String key) {
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        query.lambda().eq(SysRole::getRoleKey, key);
        return this.getOne(query, false);
    }
}
